package com.study.forumsystemproject.service.impl;

import com.study.forumsystemproject.common.Result;
import com.study.forumsystemproject.common.ResultCode;
import com.study.forumsystemproject.dao.ArticleReplyMapper;
import com.study.forumsystemproject.exception.ApplicationException;
import com.study.forumsystemproject.model.ArticleReply;
import com.study.forumsystemproject.service.IArticleReplyService;
import com.study.forumsystemproject.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * program: forum-system-project
 * <p>
 * description: 文章回复接口的实现类
 * <p>
 * author: xiaoxie
 * <p>
 * create: 2024-08-19 10:03
 **/
@Service
@Slf4j
public class ArticleReplyImpl implements IArticleReplyService {
    @Autowired
    private ArticleReplyMapper articleReplyMapper;
    @Autowired
    private IArticleService articleService;

    @Override
    public void addArticleReply(ArticleReply articleReply) {

        //参数校验
        if(articleReply == null || articleReply.getArticleId() == null
        || articleReply.getContent() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //执行回复表的插入
        //填充好默认测试,降低数据库的消耗
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(null);
        articleReply.setState(0);
        articleReply.setDeleteFlag(0);
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.fail(ResultCode.ERROR_SERVICES));
        }

        //更新article表的回复数量
        articleService.addOneReply(articleReply.getArticleId());
    }

    @Override
    public List<ArticleReply> getArticleReplyByArticleId(Long articleId) {
        if(articleId == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return  articleReplyMapper.getReply(articleId);
    }
}
